import axios from "axios";

// 获取应用信息
export const getApplicationInfo = async (apiKey: string) => {
  try {
    // const response = await axios.get("https://riscbase.hsk.top/v1/info", {
    const response = await axios.get("https://9ty87yf37921.vicp.fun/v1/info", {
      headers: {
        Authorization: `Bearer ${apiKey}` // 传递 apiKey 在请求头中
      }
    });
    return response.data; // 返回请求结果
  } catch (error) {
    console.error("请求失败:", error);
    throw error; // 抛出错误
  }
};

// 获取应用参数信息
export const getParameters = async (apiKey: string) => {
  try {
    // const response = await axios.get("https://riscbase.hsk.top/v1/parameters", {
    const response = await axios.get("https://riscbase.hsk.top/v1/parameters", {
      headers: {
        Authorization: `Bearer ${apiKey}` // 使用 apiKey 作为 Authorization 的值
      }
    });
    return response.data; // 返回参数数据
  } catch (error) {
    console.error("请求参数失败:", error);
    throw error;
  }
};

//发送消息
export async function postMessage(apiKey: string, message: string) {
  try {
    const timestamp = Date.now(); // 获取当前时间戳
    const data = {
      inputs: {},
      query: `${message}`,
      response_mode: "streaming",
      conversation_id: "",
      user: timestamp.toString() // 使用时间戳作为 user 的值
    };

    const response = await axios.post(
      "https://riscbase.hsk.top/v1/chat-messages",
      data,
      {
        headers: {
          Authorization: `Bearer ${apiKey}` // 替换 'YOUR_TOKEN' 为你的实际 token
        }
      }
    );
    const responseData = response.data;
    const matches = [...responseData.matchAll(/"thought": "(.*?)"/g)];
    if (matches.length > 0) {
      // 获取最后一个 thought
      const lastThoughtUnicode = matches[matches.length - 1][1]; // 提取最后一个 Unicode 字符串
      // 解码 Unicode 字符串
      return JSON.parse('"' + lastThoughtUnicode + '"');
    } else {
      console.log("解析失败");
    }
  } catch (error) {
    console.error("Error:", error);
  }
};

export async function postWorkflow(apiKey: string) {
  try {
    const timestamp = Date.now(); // 获取当前时间戳
    const data = {
      inputs: {},
      response_mode: "streaming",
      user: timestamp.toString() // 使用时间戳作为 user 的值
    };

    const response = await axios.post(
      "https://9ty87yf37921.vicp.fun/v1/workflows/run",
      data,
      {
        headers: {
          Authorization: `Bearer ${apiKey}` // 替换 'YOUR_TOKEN' 为你的实际 token
        }
      }
    );
    const responseData = response.data;
    // console.log("工作流结果",responseData)

    const matches = [...responseData.matchAll(/"event": "workflow_finished"[^}]*"text": "(.*?)"/g)];
    if (matches.length > 0) {
    //   // 获取最后一个 "event": "workflow_finished" 对应的字符串
      const lastEventString = matches[matches.length - 1][0]; // 获取最后一个匹配到的完整事件字符串
      // 提取 "text" 字段的值
      const textValue = lastEventString.match(/"text": "(.*?)"/);
      // // 解码 Unicode 字符串
      const decodedContent = JSON.parse('"' + textValue[1] + '"');
      return decodedContent;
    }
  } catch (error) {
    console.error("Error:", error);
  }
}
